<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>求是否为质数</title>
</head>
<body>

    <script>
        function forPrime(num){
            
            var num = parseInt(prompt('请输入一个正整数'));

            if(num <= 0 || num%1 !== 0){
                document.write(num + '这不是正整数，请刷新后重新输入');
            }else{
                //假设一个数是质数
                var isPrime = true;
                //存放约数
                var few;
                for(var i = 2;i < num; i++){
                    if(num%i === 0){
                        //出现能被整除的数时，确定它不是质数
                        isPrime = false;
                        few = i;
                        //终止循环
                        break;
                    }
                }

                //是质数则输出
                if(isPrime){
                    document.write(num + '是质数');
                }else{
                    document.write(num + '不是质数，能被' + few + '整除');
                }
            }
        }
        forPrime();
    </script>
</body>
</html>